import 'package:jd_shop/utils/ToastMsg.dart';
import 'package:json_annotation/json_annotation.dart';

// 使用@JsonSerializable注解，启用泛型参数工厂方法。
@JsonSerializable(genericArgumentFactories: true)
class BaseRes<T> {
  String desc = "";
  late int code;
  late T data;

  BaseRes(this.data);

  BaseRes.fromJson(Map<String, dynamic>? json) {
    try {
      if (json?['data'] != null && json?['data'] != 'null') {
        data = json?['data'] as T;
      }
      desc = json?['desc'] ?? "";
      code = json?['code'];
      if (code != 1) {
        ToastMsg.show(desc);
      }
    } catch (e) {
      ToastMsg.show(e);
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    if (this.data != null) {
      data['data'] = this.data;
    }
    data['code'] = this.code;
    data['desc'] = this.desc;
    return data;
  }
}
